package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Company;
import cn.hls.entity.Site;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CompanyService;
import cn.hls.service.SiteService;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: SiteController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 人员信息控制
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/site")
public class SiteController extends BaseController<Site> {
	 
	@Resource(name="siteService")
	private SiteService siteService;
	@Resource(name="companyService")
	private CompanyService companyService;
		/**
		 * 表单查看或修改
		 * @param id
		 * @param show
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/info")
		public ModelAndView info(Long id, boolean show,Site site )
				throws Exception {
			Site entity = null;
			if (id != null) {
				entity = siteService.find(id);
			}else{
				//存在企业及站点类型的数据为托运方添加或编辑的站点信息
				if(site.getCompany()!=null && site.getCompany().getId()!=null && site.getType()!=null){
					Company company=companyService.find(site.getCompany().getId());
					entity=new Site();
					entity.setType(2);//类型设置发货类型
					entity.setCompany(company);
				}
			}
			if (show) {
				return new ModelAndView("site/show").addObject("entity", entity); // 查看页面
			} else {
			
				return new ModelAndView("site/info")
						.addObject("entity", entity);// 新增、修改页面
			}

		}

		/**
		 * 添加操作
		 * 
		 * @param entity
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/add")
		@ResponseBody
		public Msg add(Site entity) throws Exception {
			if(!siteService.exists(entity)){
				if(entity.getCompany()!=null){
					if(entity.getCompany().getId()==null){
						entity.setCompany(null);
					}
				}
				boolean flag = siteService.add(entity);
				if (flag) {
					msg.setInfo("操作成功");
					msg.setStatus(200);
				} else {
					msg.setInfo("操作失败");
					msg.setStatus(201);
				}
			}else{
				msg.setInfo("存在相同的人员信息!");
				msg.setStatus(201);
			}
			return msg;
		}

		 
		/**
		 * 删除操作
		 * 
		 * @param id
		 * @return
		 * @throws Exception
		 */
		@ResponseBody
		@RequestMapping("/del")
		public Msg del(String ids) throws Exception {
			String[] tempIds = ids.split("\\,");
			siteService.delete(tempIds);
			return msg;
		}

		/**
		 * 修改操作
		 * @param entity
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/update")
		@ResponseBody
		public Msg update(Site entity) throws Exception {
			try {
				boolean flag = siteService.update(entity);
				if (flag) {
					msg.setInfo("操作成功");
					msg.setStatus(200);
				} else {
					msg.setInfo("操作失败");
					msg.setStatus(201);
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg.setInfo(e.getMessage());

			}
			return msg;
		}

		/**
		 * 分页查询
		 * 
		 * @param entity 查询的会友对象
		 * @param page 会友分页对象
		 * @param nodeIsLeaf 判断选择的节点是否分叶子节点
		 * @return PageInfo<site>
		 * @throws Exception
		 */
		@RequestMapping("/list")
		@ResponseBody
		public PageInfo<Site> list(Site entity, PageInfo<Site> page)
				throws Exception {
			return siteService.loadSiteInfo(entity, page);
		}
		
		@RequestMapping("/loadSiteList")
		@ResponseBody
		public List<Site> loadSiteList(Site entity)throws Exception{
			return siteService.loadSiteList(entity);
		}
	
		@RequestMapping("/view")
		public ModelAndView view(Site entity) {
			return new ModelAndView("/site/list").addObject("entity",entity);

		}

}
